package net.wanho.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.TransshipmentDTO;
import net.wanho.mapper.UserInfoMapper;
import net.wanho.po.Transshipment;
import net.wanho.service.TransshipmentService;
import net.wanho.mapper.TransshipmentMapper;
import net.wanho.service.UserInfoService;
import net.wanho.vo.PageInfo;
import net.wanho.vo.TransshipmentVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

/**
* @author 王洋
* @description 针对表【transshipment(调货单表)】的数据库操作Service实现
* @createDate 2024-09-03 17:43:36
*/
@Service
@RequiredArgsConstructor
public class TransshipmentServiceImpl extends ServiceImpl<TransshipmentMapper, Transshipment>
    implements TransshipmentService{
    private final UserInfoService userInfoService;
    private final TransshipmentMapper transshipmentMapper;

    @Override
    public PageInfo<TransshipmentVo> transshipmentPageList(Integer pageNum, Integer pageSize, TransshipmentDTO transshipment) {
        Page<TransshipmentVo> page = new Page<>(pageNum, pageSize);
        Page<TransshipmentVo> page2 = new Page<>(pageNum, pageSize);
        QueryWrapper<TransshipmentVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(transshipment.getSourceStoreId()),"source_store_id", transshipment.getSourceStoreId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(transshipment.getTargetStoreId()),"target_store_id", transshipment.getTargetStoreId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(transshipment.getSourceProductName()),"p1.product_name", transshipment.getSourceProductName());
        queryWrapper.eq(ObjectUtils.isNotEmpty(transshipment.getTargetProductName()),"p.product_name", transshipment.getTargetProductName());


        this.baseMapper.selectsource(page, queryWrapper);

        PageInfo<TransshipmentVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setLimitIndex((page.getCurrent()-1)*page.getSize());
        pageInfo.setResultList(page.getRecords());

        page.getRecords().forEach(item -> {
            item.setCreateName(userInfoService.getById(item.getCreateBy()).getUserCode());
        });


        return pageInfo;

    }

    @Override
    public void transshipmentAdd(TransshipmentDTO transshipmentDTO) {
        transshipmentMapper.transshipmentAdd(transshipmentDTO);

    }
}




